<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of COntactDaoImpl
 *
 * @author krisada.thiangtham
 */
require '../com.swiss.mooh.dao/ContactDao.php';
require '../com.swiss.mooh.utils/ArrayList.php';

class ContactDaoImpl implements ContactDao {

    //put your code here
    public function createContact($contactTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("INSERT INTO SWISS_MANAGE_CONTACT VALUES (?,?,?);");
        $stmt->bind_param('sss', $contactTO->getContactID(), $contactTO->getContactDescription(), $contactTO->getContactCreateDateTime());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function updateContact($contactTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("UPDATE SWISS_MANAGE_CONTACT SET CONTACT_DESC_DESC = ? WHERE CONTACT_ID LIKE ?");
        $stmt->bind_param('ss', $contactTO->getContactDescription(), $contactTO->getContactID());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function getContactContent() {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_MANAGE_CONTACT;");
        $stmt->execute();
        $detailList = new ArrayList();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $contactVO = new Contact();
            $contactVO->setContactID($x['CONTACT_ID']);
            $contactVO->setContactDescription($x['CONTACT_DESC_DESC']);
            $contactVO->setContactCreateDateTime($x['CREATED_DATE_TIME']);
            $detailList->Add($contactVO);
        }
        $stmt->close();
        return $detailList;
    }

}
